﻿//
//  HFViewModelSpec.m
//  HFBaseHTTPRequestManagerModule
//
//  Created by admin on 2020/6/29.
//  Copyright 2020 白野. All rights reserved.
//

#import <Kiwi/Kiwi.h>
#import <HFBaseHTTPRequestManagerModule/HFBaseHTTPRequestManagerModule.h>
#import "HFViewModel.h"


SPEC_BEGIN(HFViewModelSpec)

describe(@"HFViewModel", ^{
    NSString *registerMobile = @"15022221245";
    NSString *resetPasswordMobile = @"15022221244";
    NSString *loginMobile = @"15022221244";
    NSString *password = @"qq123456";
    NSString *changePassword = @"qq12345678";
    BOOL signEnabled = NO;

    HFBaseHTTPRequestManager.manager.signEnabled = signEnabled;
    [HFViewModel loadDomainWithURLHostKey:HFURLHostKeyDevelopmentDomain];
    
    context(@"1_Register", ^{
        __block NSString *code = nil;
        
        it(@"1_Init", ^{
            [HFViewModel reloadBaseParametersWithMobile:nil mark:@"m" uid:nil pjid:nil token:nil];
        });
                
        it(@"2_Verification_For_Register", ^{
            __block NSString *state = nil;
            __block id result = nil;
            
            [HFViewModel verificationForRegisterWithMobile:registerMobile completionHandler:^(BOOL success, id  _Nonnull responseObject, NSError * _Nonnull error) {
                state = [responseObject valueForKey:@"state"];
                result = [responseObject valueForKey:@"result"];
            }];
            
            [[expectFutureValue(state) shouldEventually] equal:@"0"];
            [[expectFutureValue(result) shouldEventually] beNonNil];
        });
        
        it(@"3_Verification_Code", ^{
            __block NSString *state = nil;
            __block id result = nil;
            
            [HFViewModel verificationCodeWithMobile:registerMobile completionHandler:^(BOOL success, id  _Nonnull responseObject, NSError * _Nonnull error) {
                state = [responseObject valueForKey:@"state"];
                result = [responseObject valueForKey:@"result"];
                code = result;
            }];
            
            [[expectFutureValue(state) shouldEventually] equal:@"0"];
            [[expectFutureValue(result) shouldEventually] beNonNil];
            [[expectFutureValue(code) shouldEventually] beNonNil];
        });
        
        it(@"4_Register", ^{
            __block NSString *state = nil;
            __block id result = nil;

            [HFViewModel registerWithMobile:registerMobile password:password code:code completionHandler:^(BOOL success, id  _Nonnull responseObject, NSError * _Nonnull error) {
                state = [responseObject valueForKey:@"state"];
                result = [responseObject valueForKey:@"result"];
            }];

            [[expectFutureValue(state) shouldEventually] equal:@"0"];
            [[expectFutureValue(result) shouldEventually] beNonNil];
        });
    });

    context(@"2_Reset_Password", ^{
        __block NSString *code = nil;
        __block NSString *codeToken = nil;
        
        it(@"1_Init", ^{
            [HFViewModel reloadBaseParametersWithMobile:nil mark:@"m" uid:nil pjid:nil token:nil];
        });

        it(@"2_Verification_For_Reset_Password", ^{
            __block NSString *state = nil;
            __block NSDictionary *result = nil;

            [HFViewModel verificationForResetPasswordWithMobile:resetPasswordMobile completionHandler:^(BOOL success, id  _Nonnull responseObject, NSError * _Nonnull error) {
                state = [responseObject valueForKey:@"state"];
                result = [responseObject valueForKey:@"result"];
            }];

            [[expectFutureValue(state) shouldEventually] equal:@"0"];
            [[expectFutureValue(result) shouldEventually] beNonNil];
        });
        
        it(@"3_Verification_Code", ^{
            __block NSString *state = nil;
            __block id result = nil;
            
            [HFViewModel verificationCodeWithMobile:resetPasswordMobile completionHandler:^(BOOL success, id  _Nonnull responseObject, NSError * _Nonnull error) {
                state = [responseObject valueForKey:@"state"];
                result = [responseObject valueForKey:@"result"];
                code = result;
            }];
            
            [[expectFutureValue(state) shouldEventually] equal:@"0"];
            [[expectFutureValue(result) shouldEventually] beNonNil];
            [[expectFutureValue(code) shouldEventually] beNonNil];
        });
        
        it(@"4_Confirm_Code", ^{
            __block NSString *state = nil;
            __block id result = nil;
            
            [HFViewModel confirmVerificationCodeWithMobile:resetPasswordMobile code:code completionHandler:^(BOOL success, id  _Nonnull responseObject, NSError * _Nonnull error) {
                state = [responseObject valueForKey:@"state"];
                result = [responseObject valueForKey:@"result"];
                codeToken = [result valueForKey:@"codeTk"];
            }];
            
            [[expectFutureValue(state) shouldEventually] equal:@"0"];
            [[expectFutureValue(result) shouldEventually] beNonNil];
            [[expectFutureValue(codeToken) shouldEventually] beNonNil];
        });
        
        it(@"5_Reset_Password", ^{
            __block NSString *state = nil;
            __block id result = nil;
            
            [HFViewModel resetPasswordWithMobile:resetPasswordMobile password:password code:code token:codeToken completionHandler:^(BOOL success, id  _Nonnull responseObject, NSError * _Nonnull error) {
                state = [responseObject valueForKey:@"state"];
                result = [responseObject valueForKey:@"result"];
            }];
            
            [[expectFutureValue(state) shouldEventually] equal:@"0"];
            [[expectFutureValue(result) shouldEventually] beNonNil];
        });
    });
    
    context(@"3_Login", ^{
        __block NSString *mark = nil;
        __block NSString *uid = nil;
        __block NSString *pjid = nil;
        __block NSString *token = nil;
        
        it(@"1_Init", ^{
            [HFViewModel reloadBaseParametersWithMobile:nil mark:@"m" uid:nil pjid:nil token:nil];
        });

        it(@"2_Login", ^{
            __block NSString *state = nil;
            __block NSDictionary *result = nil;

            [HFViewModel passwordLoginWithMobile:registerMobile password:password completionHandler:^(BOOL success, id  _Nonnull responseObject, NSError * _Nonnull error) {
                state = [responseObject valueForKey:@"state"];
                result = [responseObject valueForKey:@"result"];
                mark = [result valueForKey:@"mark"];
                uid = [result valueForKey:@"userId"];
                pjid = [result valueForKey:@"pjid"];
                token = [result valueForKey:@"authorization"];
            }];

            [[expectFutureValue(state) shouldEventually] equal:@"0"];
            [[expectFutureValue(result) shouldEventually] beNonNil];
            [[expectFutureValue(mark) shouldEventually] beNonNil];
            [[expectFutureValue(uid) shouldEventually] beNonNil];
            [[expectFutureValue(pjid) shouldEventually] beNonNil];
            [[expectFutureValue(token) shouldEventually] beNonNil];
        });
        
        it(@"3_Update", ^{
            [HFViewModel reloadBaseParametersWithMobile:registerMobile mark:mark uid:uid pjid:pjid token:token];
        });
        
        it(@"4_Change_Password", ^{
            __block NSString *state = nil;
            __block NSDictionary *result = nil;

            [HFViewModel changePasswordWithOldPassword:password newPassword:changePassword completionHandler:^(BOOL success, id  _Nonnull responseObject, NSError * _Nonnull error) {
                state = [responseObject valueForKey:@"state"];
                result = [responseObject valueForKey:@"result"];
            }];

            [[expectFutureValue(state) shouldEventually] equal:@"0"];
            [[expectFutureValue(result) shouldEventually] beNonNil];
        });
    });
});

SPEC_END

